package com.wc5818.wcCarInfos.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wc5818.common.AjaxResult;
import com.wc5818.wcBasicInfo.entity.WcBasicInfo;
import com.wc5818.wcCarInfos.entity.WcCarInfos;
import com.wc5818.wcCarInfos.mapper.WcCarInfosMapper;
import com.wc5818.wcComOrder.entity.WcComOrder;
import com.wc5818.wcComOrder.mapper.WcComOrderMapper;
import com.wc5818.wcWorkInfo.entity.WcWorkInfo;
import com.wc5818.wcWorkInfo.mapper.WcWorkInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-22
 */
@RestController
@RequestMapping("/wcCarInfos")
public class WcCarInfosController {
    @Autowired
    WcCarInfosMapper wcCarInfosMapper;
    @Autowired
    WcComOrderMapper wcComOrderMapper;

    @PostMapping("/addOne")
    public Object insertOne(@RequestBody WcCarInfos wcCarInfos) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        if (wcCarInfos.getId() == null) {
            if(wcCarInfosMapper.insert(wcCarInfos) > 0)
                ajaxResult.setSuccess(true);
        } else {
            if(wcCarInfosMapper.updateById(wcCarInfos) >= 0)
                ajaxResult.setSuccess((true));
        }
        return ajaxResult;
    }

    @RequestMapping(value = "/selectOne", method = RequestMethod.POST, produces = {"application/json;charsetset=UTF-8"})
    public @ResponseBody
    Object selectBasicInfo(@RequestBody WcCarInfos wcCarInfos) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        WcCarInfos wcCarInfos1 = wcCarInfosMapper.selectOne(new QueryWrapper<WcCarInfos>().eq("orderId", wcCarInfos.getOrderId()));

        if (wcCarInfos1 != null) {
            ajaxResult.setSuccess(true);
            ajaxResult.setData(wcCarInfos1);
        }
        return ajaxResult;
    }

    @GetMapping("/getCarInfoByOrderId")
    public Object getCarInfoByOrderId(String orderId) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        List list = wcCarInfosMapper.selectList(new QueryWrapper<WcCarInfos>().eq("orderId", orderId));
        if(list != null  && list.size() > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setData(list.get(0));
        }

        return ajaxResult;
    }
}
